package org.simtube.gui;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.*;

import org.simtube.query.QueryController;
import org.simtube.util.Cluster;

public class ClusterDialog extends JDialog implements ActionListener {

	private static final long serialVersionUID = 7489202348139459659L;
	private QueryController qc;
	
	// Dialog components
	
	private JPanel leftPanel;
	private JPanel rightPanel;
	
	private JLabel clusterLabel;
	
	private JPanel clusterPanel;
	private JPanel[] clusterItemPanel;
	private JLabel[] clusterTitleLabel;
	private JLabel[] clusterTagLabel;
	private JButton[] clusterViewButton;
	
	private JLabel viewLabel;
	private JTextArea viewArea;
	
	// arguments and variables
	
	private int nClusters;
	private List<Cluster> clusterList;
	
	private long start = 0;
	private long end = 0;
	
	public ClusterDialog(JFrame owner, String title, QueryController qc) {
		super(owner, title);
		this.qc = qc;
		
		initArguments();
		
		start = System.nanoTime();
		
		clusterList = qc.clusterQuery(nClusters);
		
		end = System.nanoTime();
		
		loadCompoments();
		
		this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		this.setSize(800, 600);
		this.setVisible(true);
		
	}


	private void initArguments() {
		nClusters = 10;
	}


	private void loadCompoments() {
		
		leftPanel = new JPanel(new BorderLayout());
		rightPanel = new JPanel(new BorderLayout());
		
		clusterLabel = new JLabel(nClusters + " clusters found... "
				+ (end - start) / 1000000 + "ms in total");
		leftPanel.add(clusterLabel, BorderLayout.NORTH);
		
		clusterPanel = new JPanel(new GridLayout(nClusters, 1, 3, 3));
		clusterItemPanel = new JPanel[nClusters];
		clusterTitleLabel = new JLabel[nClusters];
		clusterTagLabel = new JLabel[nClusters];
		clusterViewButton = new JButton[nClusters];
		for(int i = 0; i < nClusters; i++) {
			clusterItemPanel[i] = new JPanel(new GridLayout(3, 1, 1, 1));
			clusterTitleLabel[i] = new JLabel("Cluster " + (i + 1) + 
					" (" + clusterList.get(i).getDocItems().length + ")");
			clusterTagLabel[i] = new JLabel("Cluster tag: "
					+ qc.getTermById(clusterList.get(i).getTag()));
			JPanel tpanel = new JPanel(new GridLayout(1, 3, 3, 3));
			tpanel.add(new JPanel());
			tpanel.add(new JPanel());
			clusterViewButton[i] = new JButton("View!");
			clusterViewButton[i].addActionListener(this);
			tpanel.add(clusterViewButton[i]);
			
			clusterItemPanel[i].add(clusterTitleLabel[i]);
			clusterItemPanel[i].add(clusterTagLabel[i]);
			clusterItemPanel[i].add(tpanel);
			
			clusterPanel.add(clusterItemPanel[i]);
		}
		leftPanel.add(clusterPanel, BorderLayout.CENTER);
		
		viewLabel = new JLabel("You are viewing cluster: ");
		viewArea = new JTextArea(25, 20);
		JScrollPane textViewScroll = new JScrollPane(viewArea);
		
		rightPanel.add(viewLabel, BorderLayout.NORTH);
		rightPanel.add(textViewScroll, BorderLayout.CENTER);
		
		Container c = this.getContentPane();
		c.setLayout(new GridLayout(1, 2, 10, 0));
		c.add(leftPanel);
		c.add(rightPanel);
	}


	@Override
	public void actionPerformed(ActionEvent e) {
		JButton btn = (JButton)(e.getSource());
		for(int i = 0; i < nClusters; i++) {
			if(btn == clusterViewButton[i])
				showCluster(i);
		}
	}


	private void showCluster(int index) {
		Cluster c = clusterList.get(index);
		String txt = "";
		int[] dlist = c.getDocItems();
		
		for(int i = 0; i < dlist.length; i++) {
			txt += qc.getDocTextById(dlist[i]) + "\n\n";
		}
		viewArea.setText(txt);
		viewLabel.setText("You are viewing cluster " + (index + 1) + ": ");
	}
	
}
